import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/admin/home'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../Login.vue')
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/Admin.vue'),
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('../views/admin/Home.vue')
        },
        {
          path: 'account/employee-manage',
          name: 'employee-manage',
          component: () => import('../views/admin/account/EmployeeManage.vue')
        },
        {
          path: 'account/customer-manage',
          name: 'customer-manage',
          component: () => import('../views/admin/account/CustomerManage.vue')
        },
        {
          path: 'account/supplier-manage',
          name: 'supplier-manage',
          component: () => import('../views/admin/account/SupplierManage.vue')
        },
        {
          path: 'part/stock-manage',
          name: 'stock-manage',
          component: () => import('../views/admin/part/StockManage.vue')
        },
        {
          path: 'part/purchase',
          name: 'purchase',
          component: () => import('../views/admin/part/Purchase.vue')
        },
        {
          path: 'part/cart',
          name: 'part-cart',
          component: () => import('../views/admin/part/Cart.vue')
        },
        {
          path: 'part/repair-approve',
          name: 'repair-approve',
          component: () => import('../views/admin/part/RepairApprove.vue')
        },
        {
          path: 'part/sale-record',
          name: 'sale-record',
          component: () => import('../views/admin/part/SaleRecord.vue')
        },
        {
          path: 'part/purchase-record',
          name: 'purchase-record',
          component: () => import('../views/admin/part/PurchaseRecord.vue')
        },
        {
          path: 'repair-order-manage',
          name: 'repair-order-manage',
          component: () => import('../views/admin/RepairOrderManage.vue')
        },
        {
          path: 'finance/order-settle',
          name: 'order-settle',
          component: () => import('../views/admin/finance/OrderSettle.vue')
        },
        {
          path: 'finance/deposit-manage',
          name: 'deposit-manage',
          component: () => import('../views/admin/finance/DepositManage.vue')
        },
        {
          path: 'statement/business-report',
          name: 'business-report',
          component: () => import('../views/admin/statement/BusinessReport.vue')
        },
        {
          path: 'statement/settle-order',
          name: 'settle-order',
          component: () => import('../views/admin/statement/SettleOrder.vue')
        },
        {
          path: 'statement/arrearage-order',
          name: 'arrearage-order',
          component: () => import('../views/admin/statement/ArrearageOrder.vue')
        },
        {
          path: 'statement/dispatch-order',
          name: 'dispatch-order',
          component: () => import('../views/admin/statement/DispatchOrder.vue')
        }
      ]
    }
  ]
})

export default router 

// 路由守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('user')
  if (to.path !== '/login' && !user) {
    next('/login')
  } else if (to.path === '/login' && user) {
    next('/admin/home')
  } else {
    next()
  }
}) 